#####################################################1
import numpy as np
from scipy import stats
print("第一题")
# 这些是已知数据
confidence = 0.95
sample_mean = 12168
sample_n = 480
pop_std = 2200
sigma_debt = pop_std / np.sqrt(sample_n)
# 95%置信区间
CI_debt = stats.norm.interval(confidence, loc = sample_mean, scale = sigma_debt)
print(CI_debt)
######################################################2
print("第二题")
from scipy import stats
import numpy as np
x=[422.2,417.2,425.6,425.8,423.1,418.7,428.2,438.3,434.0,412.3,431.5,413.5,441.3,423.0,420.3]
y=np.array(x)
alpha=0.95
loc=np.mean(y)
tesm=stats.tsem(y,ddof=1)
scale=tesm/(15**0.5)
c = stats.norm.interval(alpha,loc,scale)
print(c)
##########################3
print("第三题")
import numpy as np
from scipy import stats
confidence = 0.90
pop_n = 5000
sample_n = 400
sample_reject = 32
sample_p = sample_reject/sample_n # 样本废品率为8%
# 抽样平均误差（不放回抽样）
sigma_reject = np.sqrt(sample_p*(1-sample_p)/sample_n)*np.sqrt((pop_n - sample_n)/(pop_n-1))
# 90%置信区间
CI_reject_rate = stats.norm.interval(confidence, loc = sample_p, scale = sigma_reject)
print(CI_reject_rate)
print('点估计值为',sample_p)
print('90%置信区间为',CI_reject_rate)
######################################4
print("第四题")
from scipy import stats
confidence = 0.90
sample_n = 18
sample_var = 0.36
# 计算卡方分布（自由度n-1）90%置信区间的上下限
chi2_lower, chi2_higher = stats.chi2.interval(confidence, df = sample_n -1)
# 计算((n-1)*样本方差)的值
sample_df_var = (sample_n - 1)*sample_var
# 计算得到总体方差的置信区间估计
CI_weight_var = (sample_df_var/chi2_higher,sample_df_var/chi2_lower)
print(CI_weight_var)


